<?php
class Sections_model extends MY_Model
{
	public
		$table = 'sections',
		$prKey = 'section_id',
		
		$tlink = "literature_sections",
		$tlink_key = 'sections_id';

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id' 			=> '',
			'name'			=> T_('Section name'),
			'description'	=> T_('Section description'),
			'type'			=> T_('Type'),
			'section'		=> T_('Section'),
			'subsection'	=> T_('Subsection'),
		);
			
		$this->rules = array(
			'id' 			=> "trim|integer",
			'name'	 		=> "trim|required|max_length[255]",
			'description'	=> "trim|required|max_length[1000]"
		);
	}
	
	function getSections($book_id){
		$this->db->from($this->table);
		$this->db->join($this->tlink, $this->table.'.'.$this->prKey.' = '.$this->tlink.'.'.$this->link_key, 'left');
		$this->db->where($this->tlink.'.literature_id', $book_id);
		$query = $this->db->get();

		return $query->row_array();
	}
	
	function getAllSections(){
		$this->db->from($this->table);
		$this->db->where('section_sub', '0');
		
		return $this->db->get()->result_array();
	}
	
	function search($config = array())
	{
		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');
		
		$name 			= $this->input->get_post('name');
		$description	= $this->input->get_post('description');
		
		//is not used in administrator pages filter 
		$type 	= $this->input->get_post('type');
		
		$sql = 'SELECT *';
		$sql .= ' FROM '.$this->table.' AS p';
		$sql .= ' WHERE 1 = 1 '; 
								
		if ($name){
			
			$sql .= ' AND p.section_name LIKE ' . $this->db->escape('%' . $name . '%'); 
		}
		
		if($description) {
			$sql .= ' AND p.section_descr LIKE ' . $this->db->escape('%' . $description . '%');
		}
		
		if ($type) {
			if($type == 'subsection') {
				
				$sql .= ' AND p.section_sub = 0';
			} else {
				$sql .= ' AND p.section_sub > 0';
			} 
		}
		
		switch ($order)
		{
			case 'date':
				$sql .= ' ORDER by p.section_name'; 
				break;
			
			default:
				$sql .= ' ORDER BY p.section_id';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';

		$this->data['list'] = $this->db->query($sql)->result();
	}

	function get($id = null)
	{	
		$this->db->from($this->table);
		$this->db->where('section_id', $id);
		$query = $this->db->get();

		return $query->row_array();
	}
	
	function getByAlias($alias = 'all')
	{
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('category', $this->id_category);
		$this->db->where('status', 1);
		
		if ($alias != 'all')
		{
			$this->db->where('alias', $alias);
			return $this->db->get()->row();
		}
		else
		{
			$this->db->order_by('date_created', 'desc'); 	
			return $this->db->get()->result();
		}
	}		

	function getNewsBlock($category = PAGE_CATEGORY_ADVERTISER)
	{
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('category', $category);
		$this->db->where('status', 1);
		$this->db->limit(3);
		$this->db->order_by('date_created', 'desc'); 				

		return $this->db->get()->result();
	}		
	
	function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);
		
		$error = "";
		$report = "";
		foreach ($items as $id)
		{	
			$data = $this->sections_model->get($id);
			
			$res = $this->db->delete($this->table, array($this->prKey => $id));
			
			if (is_null($res))
			{
				$this->log->writeMessage('error', T_('Error. Page was not removed.'));
				$report .= ",0"; // don't deleted
			}
			else
			{
				$this->log->writeMessage('done', T_('Page was removed.'));
				$report .= ",1"; // deleted								
			}							
		}
		
		return substr($report, 1);
	}
		
	function insert($data)
	{
		if (!empty($data))
		{
			parent::insert($data);

			if ($this->db->insert_id() > 0)
			{
				$this->log->writeMessage('done', T_('Page was added succesfully.'));
				
				return TRUE;
			}
			else
			{
				$this->log->writeMessage('error', T_('Error! Page was not created.'));
				
				return FALSE;
			}
		}		
	}

	function update($data)
	{
		if (!empty($data))
		{
			$res = parent::update($data);

			$this->log->writeMessage('done', T_('Saved succesfully.'));

			return TRUE;			
		}		
	}

   function getComments($postId = null) {
   		$sql = "SELECT *
	   			FROM".$commentsTable;
   		if($postId != null)	{ 
	   		$sql .= "WHERE bc_record_ID=".$postId;
   		} 
   		
   		
   }	
   
function getSelectOptions($id_section = null)
	{
		
		$options = array();

		if ($id_section == 'all')
		{
			$this->db->where('section_sub', 0);
		}
		else if ($id_section != null)
		{
			$this->db->where('section_sub', $id_section);
		}
		else 
		{
			return "";
		}
		
		$this->db->order_by('section_name');
		$query = $this->db->get($this->table);
		
		foreach ($query->result() as $row)
		{
			$options[$row->section_id] = $row->section_name; 
		}
		
		return $options;
	}
}
?>